/*
 * 
 * index页面的JS
 * 2016年3月2日
 * 
 */


var lastId = $("#content_lastid").val();
var countChat;

function pageOnload() {
	// getIndexCharRoom();
	createUser();
	getChatContent();

	// 开始长轮询
	getNewChatContent();
}

function createUser() {
	// http://pv.sohu.com/cityjson?ie=utf-8
	$(".now_user").html(
			"<label class='chat_user' >来自" + returnCitySN.cname + "的游客 ip: " + returnCitySN.cip+"</label>");
}
function getIndexCharRoom() {
	getPublicCRList();
}

//获取聊天室列表
function getPublicCRList() {
	$.ajax({
		type : "POST",
		url : "/getPublicCRList",
		data : {},

		success : function(data) {
			for (var i = 0; i < data.length; i++)
				$("#left_div").append(
						"<div class='public_CRList'><label>" + data[i].roomName
								+ "</label></div>");

		}

	});
}

function getChatContent() {

	countChat = parseInt($("#count_chat").html());
	
	$.ajax({
				type : "POST",
				url : "/getChatContent",
				data : {"roomNum" : 1},

				success : function(data) {

					for (var x = 0; x < data.length; x++)
						if (data[x][3] != (returnCitySN.cip))
							$("#chat_content_ul").append(
									"<li class='chat_content'><label class='chat_user' >" + data[x][2] + "  ip "+data[x][3]
											+ "  </label><br><label class='chat_user_content' >" + data[x][6] + "</label></li>");
						else
							$("#chat_content_ul").append(
									"<li class='chat_content me'><label class='chat_user' >" + data[x][2]+ "  ip "+data[x][3]
											+ "  </label><br><label class='chat_user_content' >" + data[x][6] + "</label></li>");

					lastId = data[data.length - 1][0];
					
					 $("content_lastid").val(lastId);
					 
					 countChat += 15;
					 
					
					 $("#count_chat").html(countChat);
					 
					$("#chat_content_ul").scrollTop(
							$("#chat_content_ul")[0].scrollHeight);
				}
			});
}
// 获取新消息
function getNewChatContent() {
	$.ajax({
				type : "POST",
				url : "/getNewChatContent",
				data : {
					"lastId" : lastId,
					"roomNum" : 1
				},
				timeout : 5000,
				success : function(data) {

					for (var x = 0; x < data.length; x++)
						if (data[x][3] != (returnCitySN.cip))
							$("#chat_content_ul").append(
									"<li class='chat_content'><label class='chat_user' >" + data[x][2]+ "  ip "+data[x][3]
											+ "  </label><br><label class='chat_user_content' >" + data[x][6] + "</label></li>");
						else
							$("#chat_content_ul").append(
									"<li class='chat_content me'><label class='chat_user' >" + data[x][2]+ "  ip "+data[x][3]
											+ "  </label><br><label class='chat_user_content' >" + data[x][6] + "</label></li>");

					$("#chat_content_ul").scrollTop(
							$("#chat_content_ul")[0].scrollHeight);
					if(data.length>0)
						lastId = data[data.length - 1][0];
					
					countChat += data.length;
					
					 $("#count_chat").html(countChat);
					 
					$("content_lastid").val(lastId);

					getNewChatContent();// 循环
				},
				error : function() {
					getNewChatContent();// 循环
				}
			});
}

function submitChat() {
	var content = $("#chat_input").val();
	if (content == "")
		return;
	var user = returnCitySN.cname + "游客";
	var ip =  returnCitySN.cip;
	$.ajax({
		type : "POST",
		url : "submitChat",
		data : {
			"chatrecord.nickname" : user,
			"chatrecord.chatcontent" : content,
			"chatrecord.userid" : 0,
			"chatrecord.roomnum" : 1,
			"chatrecord.ip":ip,
			"chatrecord.targetuserid":0
		},

		success : function(data) {
			$("#chat_content_ul").scrollTop(
					$("#chat_content_ul")[0].scrollHeight);
			$("#chat_input").val("");
		}
	});
}

function keydownMsg(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
	keyCode = evt.keyCode ? evt.keyCode
			: (evt.which ? evt.which : evt.charCode);
	if (keyCode == 13) {
		submitChat();
	}
}
